package com.wut.clock.factory.number;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wut.clock.common.constant.CommentConstant;
import com.wut.clock.common.util.RedisKeyUtil;
import com.wut.clock.entity.Comment;
import com.wut.clock.mapper.CommentMapper;
import com.wut.clock.mongo.repository.CollectionRepository;
import com.wut.clock.vo.portal.ArticleCountVo;
import com.wut.clock.vo.portal.CommentCountVo;
import com.wut.clock.vo.portal.UserCountVo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ZYH
 * @unit D9Lab
 * @description 统计文章的浏览量、评论数、收藏数、点赞数和点踩数
 * @date 2022/6/13 16:00
 */
@SuppressWarnings("unchecked")
@Component
public class ArticleCount implements Number {

    @Resource
    private CommentMapper commentMapper;

    @Resource
    private CollectionRepository collectionRepository;

    @Resource
    private RedisTemplate redisTemplate;


    @Override
    public ArticleCountVo countArticleNumber(Integer articleId) {
        // TODO 获得浏览量

        Integer entityType = CommentConstant.TopicType.ARTICLE.getCode();
        // 评论数
        List<Comment> commentList = commentMapper.selectList(new QueryWrapper<Comment>().
                eq("topic_type", entityType).
                eq("topic_id", articleId));
        Integer commentCount = commentList==null? 0:commentList.size();
        // 收藏数
        int collectionCount = collectionRepository.countByArticleId(articleId);

        // 点赞数
        String entityLikeKey = RedisKeyUtil.getEntityLikeKey(articleId, entityType);
        Long likeCount = redisTemplate.opsForValue().size(entityLikeKey);

        // TODO 点踩数

        ArticleCountVo articleCountVo = new ArticleCountVo();
        articleCountVo.setCommentCount(commentCount);
        articleCountVo.setCollectionCount(collectionCount);
        articleCountVo.setLikeCount(likeCount==null ? 0:likeCount);

        return articleCountVo;
    }

    @Override
    public UserCountVo countUserNumber(Integer userId) {
        return null;
    }

    @Override
    public CommentCountVo countCommentNumber(Integer commentId) {
        return null;
    }
}
